﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YSC_Core.BLL.Domain.CommonModels;
using YSC_Core.BLL.Domain.Utils;
using YSC_Core.BLL.Repository;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services.Interfaces;

namespace YSC_Services.BLL.Services.Implementations
{
    /// <summary>
    /// 基础数据同步接口实现
    /// </summary>
    public class BasisDataUpdateAppServices : IBasisDataUpdateAppServices
    {
        private IEfRepository _rep;
        public BasisDataUpdateAppServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 鱼种信息同步
        /// </summary>
        /// <returns></returns>
        public ReturnValueModel FishSpeciesUpdate()
        {
            var list = _rep.Where<FishSpecies>(p => p.Description != "统货").DistinctBy(p => p.FishName).ToList();

            List<FishSpecies> fishAll = new List<FishSpecies>();

            foreach (var item in list)
            {
                FishSpecies fishSpecies = new FishSpecies()
                {
                    Id = Guid.NewGuid().ToString(),
                    Type = item.Type,
                    FishName = item.FishName,
                    Description = "统货",
                    FishCode = string.Format(item.FishCode + "123"),
                    Status = item.Status,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f",
                };
                fishAll.Add(fishSpecies);
            }

            _rep.InsertList(fishAll);

            return new ReturnValueModel { Code = 200, Msg = "OK" };
        }

        /// <summary>
        /// 产地市场信息同步
        /// </summary>
        /// <returns></returns>
        public ReturnValueModel PlaceOfOriginUnitUpdate(string name)
        {
            if(string.IsNullOrEmpty(name))
                return new ReturnValueModel { Code = 400, Msg = "请填写Name" };


            var unitAll = _rep.Where<PlaceOfOriginUnit>(p => p.Remarks == name).ToList();

            var userAll = _rep.Where<YSC_Core.BLL.Domain.Entity.YscUser>(p => p.Remarks == name).ToList();

            List<UserUnit> userUnitAll = new List<UserUnit>();

            // userAll.ForEach(p => _rep.Remove(_rep.Where<UserUnit>(x => x.UnitId == p.Id).ToList()));

            foreach (var item in unitAll)
            {
                UserUnit userUnit = new UserUnit()
                {
                    Id = Guid.NewGuid().ToString(),
                    UserId = userAll.Where(p => p.UserName == item.PlaceOriginName).FirstOrDefault().Id,
                    UnitId = item.Id,
                    UnitType = "b7770211-c4a8-11e9-a7b7-48d53943449f",
                    Remarks = name,
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = "4076e59b-c219-11e9-a7b7-48d53943449f"
                };
                userUnitAll.Add(userUnit);
            }

            var total = unitAll.ToList();
            var total1 = userUnitAll.Count();


            _rep.InsertList(userUnitAll);

            return new ReturnValueModel { Code = 200, Msg = "OK" };
        }
    }
}
